home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload Trio 2 / Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO / dir27 / calctool.zip / NEWS.C < prev    next >
C/C++ Source or Header  |  1992-09-09  |  8KB  |  344 lines

  1.  
  2. /*  @(#)news.c 1.9 89/12/21
  3.  *
  4.  *  These are the NeWS dependent graphics routines used by calctool.
  5.  *
  6.  *  Copyright (c) Rich Burridge.
  7.  *                Sun Microsystems, Australia - All rights reserved.
  8.  *
  9.  *  Permission is given to distribute these sources, as long as the
  10.  *  copyright messages are not removed, and no monies are exchanged.
  11.  *
  12.  *  No responsibility is taken for any errors or inaccuracies inherent
  13.  *  either to the comments or the code of this program, but if
  14.  *  reported to me then an attempt will be made to fix them.
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include <strings.h>
  19. #include "calctool.h"
  20. #include "color.h"
  21. #include "extern.h"
  22. #include <sys/types.h>
  23.  
  24. extern FILE *PostScript ;
  25. extern FILE *PostScriptInput ;
  26.  
  27.  
  28. clear_canvas(canvas, color)
  29. enum can_type canvas ;
  30. int color ;
  31. {
  32.   char cantype ;
  33.  
  34.        if (canvas == KEYCANVAS) cantype = 'K' ;
  35.   else if (canvas == REGCANVAS) cantype = 'R' ;
  36.   FPRINTF(PostScript,"%d %cC PSClearCanvas\n", color, cantype) ;
  37.   FFLUSH(PostScript) ;
  38. }
  39.  
  40.  
  41. close_frame()
  42. {
  43.   FPRINTF(PostScript, "PSCloseFrame\n") ;
  44.   FFLUSH(PostScript) ;
  45. }
  46.  
  47.  
  48. color_area(x, y, width, height, color)
  49. int x, y, width, height, color ;
  50. {
  51.   FPRINTF(PostScript, "%d %d %d %d %d PSColorArea\n",
  52.           color, x, width, height, y) ;
  53.   FFLUSH(PostScript) ;
  54. }
  55.  
  56.  
  57. create_menu(mtype)     /* Create popup menu for right button press. */
  58. enum menu_type mtype ;
  59. {
  60.   int i ;
  61.  
  62.   FPRINTF(PostScript, "/Menu%1d [\n", (int) mtype) ;
  63.   for (i = 0; i < MAXREGS; i++)
  64.     {
  65.       switch (mtype)
  66.         {
  67.           case M_ACC    :                              /* Accuracies. */
  68.           case M_EXCH   :                              /* Register exchange. */
  69.           case M_LSHIFT :                              /* Left shift. */
  70.           case M_RCL    :                              /* Register recall. */
  71.           case M_RSHIFT :                              /* Right shift. */
  72.           case M_STO    :                              /* Register store. */
  73.                           FPRINTF(PostScript, "(%s)", num_names[i]) ;
  74.                           break ;
  75.           case M_CON    :                              /* Constants. */
  76.                           FPRINTF(PostScript, "(%s)", con_names[i]) ;
  77.                           break ;
  78.           case M_FUN    :                              /* Functions. */
  79.                           FPRINTF(PostScript, "(%s)", fun_names[i]) ;
  80.                           break ;
  81.         }
  82.       FPRINTF(PostScript, " { %1d typedprint }\n", (int) mtype) ;
  83.     }
  84.   FPRINTF(PostScript, "] /new DefaultMenu send def\n") ;
  85.   FFLUSH(PostScript) ;
  86. }
  87.  
  88.  
  89. destroy_frame()
  90. {
  91.   exit(0) ;
  92. }
  93.  
  94.  
  95. do_menu(mtype)      /* Popup appropriate menu and get value. */
  96. enum menu_type mtype ;
  97. {
  98.   int mval ;
  99.  
  100.   FPRINTF(PostScript, "{ /ParentMenu Menu%1d def } ClientMenu send\n",
  101.                       (int) mtype) ;
  102.   FFLUSH(PostScript) ;
  103.   pscanf(PostScriptInput, "%d", &mval) ;
  104.   return(mval) ;
  105. }
  106.  
  107.  
  108. drawline(x1, y1, x2, y2)
  109. int x1, y1, x2, y2 ;
  110. {
  111.   FPRINTF(PostScript, "%d %d %d %d PSDrawLine\n", x1, x2, y1, y2) ;
  112.   FFLUSH(PostScript) ;
  113. }
  114.  
  115.  
  116. draw_regs()
  117. {
  118.   FPRINTF(PostScript, "PSDrawRegs\n") ;
  119.   FFLUSH(PostScript) ;
  120. }
  121.  
  122.  
  123. drawtext(x, y, window, fontno, color, str)
  124. enum can_type window ;
  125. enum font_type fontno ;
  126. int x, y, color ;
  127. char *str ;
  128. {
  129.   int i ;
  130.   char font, fonttype[6], line[MAXLINE] ;
  131.  
  132.        if (fontno == SFONT) STRCPY(fonttype, "SFont") ;
  133.   else if (fontno == NFONT) STRCPY(fonttype, "NFont") ;
  134.   else if (fontno == BFONT) STRCPY(fonttype, "BFont") ;
  135.        if (window == KEYCANVAS) font = 'K' ;
  136.   else if (window == REGCANVAS) font = 'R' ;
  137.   line[0] = '\0' ;
  138.   for (i = 0; i < strlen(str); i++)
  139.     switch (str[i])
  140.       {
  141.         case '\\' : STRCAT(line,"\\\\") ;
  142.                     break ;
  143.         case '('  : STRCAT(line,"\\(") ;
  144.                     break ;
  145.         case ')'  : STRCAT(line,"\\)") ;
  146.                     break ;
  147.         default   : STRNCAT(line, &str[i], 1) ;
  148.       }
  149.   FPRINTF(PostScript, "(%s) %d %cCHeight %d ", line, x, font, y) ;
  150.   FPRINTF(PostScript, "%s %d %cC PSMakeText\n", fonttype, color, font) ;
  151.   FFLUSH(PostScript) ;
  152. }
  153.  
  154.  
  155. get_display()         /* GET function key was pressed. */
  156. {
  157. }
  158.  
  159.  
  160. get_next_event()
  161. {
  162.   int type ;
  163.  
  164.   FFLUSH(PostScript) ;
  165.   if (pscanf(PostScriptInput, "%d", &type) == EOF) destroy_frame() ;
  166.   switch (type)
  167.     {
  168.       case KEYBOARD    : pscanf(PostScriptInput, "%d", &cur_ch) ;
  169.       case LEFT_DOWN   :
  170.       case MIDDLE_DOWN :
  171.       case LEFT_UP     :
  172.       case MIDDLE_UP   : pscanf(PostScriptInput, "%d%d", &curx, &cury) ;
  173.     }
  174.   return(type) ;
  175. }
  176.  
  177.  
  178. handle_selection()      /* Handle the GET function key being pressed. */
  179. {
  180. }
  181.  
  182.  
  183. init_fonts()
  184. {
  185.   FPRINTF(PostScript, "PSInitFonts\n") ;
  186.   FFLUSH(PostScript) ;
  187.   nfont_width = 9 ;
  188. }
  189.  
  190.  
  191. init_ws_type()
  192. {
  193.   gtype = NEWS ;
  194.   if (ps_open_PostScript() < 0) return -1 ;
  195.   if (send_ps_file(NEWSFILE) == -1)
  196.     {
  197.       FCLOSE(PostScript) ;
  198.       return(-1) ;
  199.     }
  200.   FFLUSH(PostScript) ;
  201.   if (ferror(PostScript))
  202.     {
  203.       FCLOSE(PostScript) ;
  204.       return(-1) ;
  205.     }
  206.   FPRINTF(PostScript, "PSIsColor\n") ;
  207.   FFLUSH(PostScript) ;
  208.   pscanf(PostScriptInput, "%d", &iscolor) ;
  209.   FPRINTF(PostScript, "PSInitialise\n") ;
  210.   FFLUSH(PostScript) ;
  211.   return(0) ;
  212. }
  213.  
  214.  
  215. load_colors()      /* Create and load calctool color map. */
  216. {
  217.   u_char red[CALC_COLORSIZE], green[CALC_COLORSIZE], blue[CALC_COLORSIZE] ;
  218.   int i ;
  219.  
  220.   calc_colorsetup(red, green, blue) ;
  221.   FPRINTF(PostScript, "%d PSMakeColorTable\n", CALC_COLORSIZE) ;
  222.   for (i = 0; i < CALC_COLORSIZE; i++)
  223.     FPRINTF(PostScript, "%d %d %d %d PSLoadColor\n",
  224.                          i, blue[i], green[i], red[i]) ;
  225.   FFLUSH(PostScript) ;
  226. }
  227.  
  228.  
  229. /*ARGSUSED*/
  230. make_frames(argc, argv)
  231. int argc ;
  232. char *argv[] ;
  233. {
  234.   FPRINTF(PostScript, "%d %d %d %d %d %d %d PSMakeFrames\n",
  235.                      wx, wy, TWIDTH, DISPLAY+THEIGHT,
  236.                      ix, iy, iconic) ;
  237.   FFLUSH(PostScript) ;
  238. }
  239.  
  240.  
  241. make_icon()
  242. {
  243.   int depth, i, j, k, n ;
  244.   char line[MAXLINE], name[MAXLINE], v[16][5] ;
  245.   FILE *fin ;
  246.  
  247.   if (iscolor) STRCPY(name, "calctool.color.icon") ;
  248.   else STRCPY(name, "calctool.icon") ;
  249.   if ((fin = fopen(name, "r")) == NULL)
  250.     {
  251.       FPRINTF(stderr, "%s: can't open %s.\n", progname, name) ;
  252.       exit(1) ;
  253.     }
  254.   depth = (iscolor) ? 8 : 1 ;
  255.   FPRINTF(PostScript, "/calctoolIcon %d %d %1d { } { <\n",
  256.                       ICONWIDTH, ICONHEIGHT, depth) ;
  257.   FGETS(line, MAXLINE, fin) ;
  258.   FGETS(line, MAXLINE, fin) ;
  259.   if (iscolor)
  260.     {
  261.       for (i = 0; i < 64; i++)
  262.         for (j = 0; j < 4; j++)
  263.           {
  264.             FGETS(line, MAXLINE, fin) ;
  265.             for (k = 0; k < 16; k++)
  266.               {
  267.                 if (!(k % 2)) STRNCPY(v[k], &line[(k/2)*7+3], 2) ;
  268.                 else STRNCPY(v[k], &line[(k/2)*7+5], 2) ;
  269.                 v[k][2] = '\0' ;
  270.               }    
  271.                  if (j == 0 || j == 1) n = 16 ;
  272.             else if (j == 2) n = 10 ;
  273.             if (j != 3)
  274.               {
  275.                 for (k = 0; k < n; k++)
  276.                   FPRINTF(PostScript, "%.2s ", v[k]) ;
  277.                 FPRINTF(PostScript, "\n") ;
  278.               }
  279.           }
  280.     }
  281.   else
  282.     {
  283.       for (i = 0; i < 32; i++)
  284.         {
  285.           FGETS(line, MAXLINE, fin) ;
  286.           for (j = 0; j < 8; j++)
  287.             {
  288.               STRNCPY(v[j], &line[j*7+3], 4) ;
  289.               v[j][4] = '\0' ;
  290.             }
  291.           FPRINTF(PostScript, "%.4s %.4s %.4s %.4s %.4s %.4s\n",
  292.                               v[0], v[1], v[2], v[4], v[5], v[6]) ;
  293.         }
  294.     }
  295.   FPRINTF(PostScript, "> } buildimage def\n") ;
  296.   FFLUSH(PostScript) ;
  297.   FCLOSE(fin) ;
  298. }
  299.  
  300.  
  301. make_items()       /* Null routine. */
  302. {}
  303.  
  304.  
  305. make_subframes()   /* Null routine. */
  306. {}
  307.  
  308.  
  309. send_ps_file(fname)
  310. char *fname ;
  311. {
  312.   FILE *stream ;
  313.   int c ;
  314.  
  315.   if ((stream = fopen(fname,"r")) == NULL) return -1 ;
  316.   while ((c = getc(stream)) != EOF) PUTC(c, PostScript) ;
  317.   FCLOSE(stream) ;
  318.   return 0 ;
  319. }
  320.  
  321.  
  322. set_cursor(type)
  323. int type ;
  324. {
  325.   FPRINTF(PostScript, "%d PSSetCursor\n", type) ;
  326.   FFLUSH(PostScript) ;
  327. }
  328.  
  329.  
  330. start_tool()
  331. {
  332.   while (1)
  333.     process_event(get_next_event()) ;
  334. }
  335.  
  336.  
  337. toggle_reg_canvas()
  338.  
  339. {
  340.   rstate = !rstate ;
  341.   FPRINTF(PostScript, "%d PSToggleRegCanvas\n", rstate) ;
  342.   FFLUSH(PostScript) ;
  343. }
  344.